
var recycleId;
var clothesNumber = [];
var booksNumber = [];
var body= $("body");
//查询捐赠记录
$("#addAarea").click(function () {

    var name;
    var province = $("#province option:selected").val();
    var city = $("#city option:selected").val();
    var county = $("#county option:selected").val();
    var neighbourhood = $("#neighbourhood").val();
    city = city+county;
    if(city===""){
        name = province;
    }else if(neighbourhood!==""){
        name = neighbourhood;
    }else {
        name = city;
    }
    var table = $("table tr:gt(0)");
    table.hide();
    var $d = table.filter(":contains('"+$.trim(name)+"')");
    $d.show();
});


body.on('click','.adminInfo',function () {
    var adminId = $(this).text();
    $.ajax({
        type: 'post',
        url: 'getAdminInfo',
        data:{
            'adminId':adminId
        },
        success:function (data) {
            var adminBody = $("#adminBody");
            adminBody.empty();
            $ul = $("<ul class='list-group'></ul>");
            $ul.appendTo(adminBody);
            $ul.append("<li class='list-group-item'>姓名："+data.adminName+"</li>");
            $ul.append("<li class='list-group-item'>手机号："+data.phone+"</li>");
            $ul.append("<li class='list-group-item'>处理订单数："+data.oderNumber+"</li>");
            $ul.append("<li class='list-group-item'>地址："+data.address+"</li>");
            $ul.append("<li class='list-group-item'>工作区域："+data.workArea+"</li>");
        }
    })
});

body.on('click','.userInfo',function () {
    var userId = $(this).text();
    $.ajax({
        type: 'post',
        url: 'getUserInfo',
        data:{
            'userId':userId
        },
        success:function (data) {
            var userBody = $("#userBody");
            userBody.empty();
            $ul = $("<ul class='list-group'></ul>");
            $ul.appendTo(userBody);
            $ul.append("<li class='list-group-item'>姓名："+data.realName+"</li>");
            $ul.append("<li class='list-group-item'>手机号："+data.phone+"</li>");
            $ul.append("<li class='list-group-item'>地址："+data.address+"</li>");
        }
    })
});

//入库分拣并生成记录
body.on('click','.sorting',function () {
    var recycleId = $(this).parent().parent().children().eq(0).text();
    $(this).attr("disabled","disabled");
    $.ajax({
        type:'get',
        url:'sortingDonation',
        data: {
            'recycleId':recycleId,
            'clothesNumber':clothesNumber[recycleId],
            'booksNumber':booksNumber[recycleId]
        },
        success:function (data) {
            console.log(data);
            location.href = data;
            $.dialog({
                title: '分拣完成！',
                content: '二维码表格已经下载！需重复下载请点击下载按钮！'
            });
            $('#table').bootstrapTable('refresh');
        }
    })

});

//下载二维码
body.on('click', '.download', function () {
    let recycleId = $(this).parent().parent().children().eq(0).text();
    $.ajax({
        url: 'download',
        type: 'get',
        data: {
            id: recycleId
        },
        success: function (res) {
            console.log(res);
            location.href = res;
        }
    })
})

    $('#table').bootstrapTable({
        classes: 'table table-bordered table-hover table-striped',
        url: 'getDonationList',
        method: 'get',
        contentType:'application/json',
        dataType : 'json',        // 因为本示例中是跨域的调用,所以涉及到ajax都采用jsonp,
        uniqueId: 'recycleId',
        idField: 'recycleId',             // 每行的唯一标识字段
        cache: false,
        toolbar: '#toolbar',       // 工具按钮容器
        clickToSelect: true,     // 是否启用点击选中行
        showColumns: true,         // 是否显示所有的列
        showRefresh: true,         // 是否显示刷新按钮
        //showToggle: true,        // 是否显示详细视图和列表视图的切换按钮(clickToSelect同时设置为true时点击会报错)
        pagination: true,                    // 是否显示分页
        sortOrder: "asc",                    // 排序方式
        sidePagination: "client",            // 分页方式：client客户端分页，server服务端分页
        pageNumber: 1,                       // 初始化加载第一页，默认第一页
        pageSize: 10,                        // 每页的记录行数     // 可供选择的每页的行数
        search: true,                      // 是否显示表格搜索，此搜索是客户端搜索

        //showExport: true,        // 是否显示导出按钮, 导出功能需要导出插件支持(tableexport.min.js)
        //exportDataType: "basic", // 导出数据类型, 'basic':当前页, 'all':所有数据, 'selected':选中的数据

        columns: [{
            field: 'recycleId',
            title: 'ID',
            sortable: true    // 是否排序
        }, {
            field: 'userId',
            title: '捐赠人Id',
            sortable: true,
            formatter:function (value, item, index) {
                return "<button class='btn btn-default btn-sm userInfo' data-toggle='modal' data-target='#userInfo'>"+value+"</button>";
            }
        }, {
            field: 'workId',
            title: '工作人员',
            sortable: true,
            formatter:function (value) {
                return "<button class='btn btn-default btn-sm adminInfo' data-toggle='modal' data-target='#adminInfo'>"+value+"</button>";

            }
        }, {
            field: 'recycleWeight',
            title: '衣服重量',
            sortable: true,
            formatter:function (value) {
                return value+"kg";
            }
        }, {
            field: 'clothesNumber',
            title: '衣服数量',
            sortable: true,
            formatter:function (value) {
                return value+"件";
            }
        }, {
            field: 'recycleNumber',
            title: '书籍数量',
            sortable: true,
            formatter:function (value) {
                return value+"本";
            }
        }, {
            field: 'recycleTime',
            title: '捐赠时间',
            sortable: true,
            formatter:function (value) {
                return new Date(+new Date(new Date(value).toJSON()) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
            }
        },{
            field: 'recycleArea',
            title: '小区地址'
        },{
            field: 'state',
            title: '操作',
            sortable: true,
            formatter:function (value,item) {
                clothesNumber[item.recycleId] = item.clothesNumber;
                booksNumber[item.recycleId] = item.recycleNumber;
                if(value==="未分拣"){
                    return "<button class='btn btn-info btn-sm sorting'>入库分拣</button>";
                }else {
                    return "<button class='btn btn-success btn-sm download'>下载二维码表格</button>";
                }
            }
        }],
        onLoadSuccess: function(data){
            $("[data-toggle='tooltip']").tooltip();
        }
    });

